package cn.jbolt.wechat.service;

import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * JBolt中处理公众号菜单核心类
 *
 * @ClassName: WechatMenuEventProcesser
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2019年7月6日
 */
public class JBoltWechatMenuEventProcesser {
    @Inject
    private JBoltWechatReplyContentService wechatReplyContentService;

    /**
     * 事件处理
     *
     * @param appId
     * @param msg
     * @return
     */
    public WxMpXmlOutMessage process(String appId, WxMpXmlMessage msg) {
        String eventKey = msg.getEventKey();
        if (StrKit.notBlank(eventKey) && eventKey.startsWith("keywords_") && eventKey.length() > 9) {
            return processKeywordsAutoReplyEvent(appId, msg);
        }
        String event = msg.getEvent();
        WxMpXmlOutMessage outMsg = null;
        //根据具体的event 跳转处理方法 没有的扩展按照下方自己搞定
        switch (event) {
            case "CLICK"://CLICK菜单
                outMsg = processClickEvent(appId, msg);
                break;
            case "VIEW"://view跳转链接的菜单
                outMsg = processViewEvent(appId, msg);
                break;
            case "pic_weixin"://微信相册发图
                outMsg = processPicWechatEvent(appId, msg);
                break;
        }
        return outMsg;
    }

    /**
     * 处理菜单事件中点击弹出微信相册选图
     *
     * @param appId
     * @param inMenuEvent
     * @return
     */
    private WxMpXmlOutMessage processPicWechatEvent(String appId, WxMpXmlMessage inMenuEvent) {
        return null;
    }

    /**
     * 处理菜单事件中选择触发关键词的菜单事件
     *
     * @param appId
     * @param msg
     * @return
     */
    private WxMpXmlOutMessage processKeywordsAutoReplyEvent(String appId, WxMpXmlMessage msg) {
        String eventKey = msg.getEventKey();
        if (StrKit.isBlank(eventKey) || eventKey.startsWith("keywords_") == false || eventKey.length() <= 9) {
            return null;
        }
        String keywords = eventKey.split("_")[1];
        if (StrKit.isBlank(keywords)) {
            return null;
        }
        //最后直接把处理这个特殊菜单触发事件 转接给系统内的关键词自动回复业务处理
        return wechatReplyContentService.getWechcatKeywordsOutMsg(appId, keywords, msg.getFromUser());
    }

    /**
     * 处理Event是VIEW的菜单事件
     *
     * @param appId
     * @param inMenuEvent
     * @return
     */
    private WxMpXmlOutMessage processViewEvent(String appId, WxMpXmlMessage inMenuEvent) {
        return null;
    }

    /**
     * 处理Event是CLICK的菜单事件
     *
     * @param appId
     * @param inMenuEvent
     * @return
     */
    private WxMpXmlOutMessage processClickEvent(String appId, WxMpXmlMessage inMenuEvent) {
        String eventKey = inMenuEvent.getEventKey();
        WxMpXmlOutMessage outMsg = null;
        switch (eventKey) {
            case "jbolt":
                outMsg = processJboltEvent();
                break;
        }
        return outMsg;
    }

    /**
     * 处理EventKey是jbolt的事件
     *
     * @return
     */
    private WxMpXmlOutMessage processJboltEvent() {

        return WxMpXmlOutMessage.TEXT().content("<a href='http://jbolt.cn'>点击进入JBolt官方网站</a>").build();
    }

}
